/*
 * IdentificationSender.cpp
 *
 *  Created on: 27/06/2014
 *      Author: Caina
 */

#include "IdentificationSender.h"

IdentificationSender::IdentificationSender() :
		socket(UDPSocket(0)) {
}

IdentificationSender::~IdentificationSender() {
}

void IdentificationSender::run() {
	ByteQueue queue;
	queue.push<char>(MessageTypes::IDENTIFIER);
	queue.push<unsigned int>(Globals::peer.get().get_identifier());
	int counter = 0;

	Globals::peers.lock();
	while (Globals::active.get() && (!Globals::identified.get() || (Globals::peers.get().size() <= 1 && counter < 3))) {
		Globals::peers.unlock();
		socket.send(queue.get_raw(), "255.255.255.255", 7778);

		SDL_Delay(4000);
		Globals::peers.lock();
		counter++;
	}
	Globals::peers.unlock();
	
	if(counter == 3){
		printf("Sou o unico no da rede\n");
	}
}
